# -*- coding: utf-8 -*-
##Report Z values for features
import ogr, os

inDS_path = ""

while(not os.path.exists(inDS_path)):
    inDS_path = raw_input("Enter the input shapefile path: ")

inDS = ogr.Open(inDS_path)
inLYR = inDS.GetLayer()

print "Name: " + inLYR.GetName()
print "Extent: " + str(inLYR.GetExtent())
print "Feature Count: " + str(inLYR.GetFeatureCount())
print "Spatial Reference: " + str(inLYR.GetSpatialRef())

inFTR = inLYR.GetNextFeature()
while(inFTR):
    print "    FID: " + str(inFTR.GetFID())

    inGEOM = inFTR.GetGeometryRef()
    
    print "    Geometry Name: " + str(inGEOM.GetGeometryName())
    print "    Geometry Point Count: " + str(inGEOM.GetPointCount())

    minz = 0
    maxz = 0
    totz = 0
    firstz = inGEOM.GetZ(0)
    lastz = inGEOM.GetZ(inGEOM.GetPointCount() - 1)
    count = inGEOM.GetPointCount()
    for pt in range(count):
        z = inGEOM.GetZ(pt)
        if totz == 0:
            minz = z
            maxz = z
            
        if z < minz:
            minz = z
        if z > maxz:
            maxz = z
        totz = totz + z
    avez = totz/count
    print "        Min Z: " + str(minz)
    print "        Max Z: " + str(maxz)
    print "        Ave Z: " + str(avez)
    print "        First Z: " + str(firstz)
    print "        Last Z: " + str(lastz)
    inFTR = inLYR.GetNextFeature()
    
print "Completed"
